/**
 * 登录脚本
 * 2019-12-18
 * by Molly
 */
try {
  var prevUrl = document.referrer || '';
  if (
    prevUrl != '' &&
    prevUrl.indexOf(MALL.Common.url.path_login) < 0 &&
    prevUrl.indexOf(MALL.Common.url.path_reg) < 0
  ) {
   var prevUrlArray = prevUrl.indexOf('www.anta.com') > 0 ? prevUrl.split('www.anta.com') : [];
   if (prevUrlArray.length > 1) {
     prevUrl = prevUrlArray[prevUrlArray.length - 1];
   }
   sessionStorage.setItem('anta_pc_before_login', prevUrl);
  }
  
} catch (error) {
  console.error(error);
}

 var J = {
     USER_INPUT_EMPTY: '请输入帐号',
     USER_INPUT_ERROR: '用户名不正确',
     USER_INPUT_PHONE_EMPTY: '请输入手机号',
     USER_INPUT_PHONE_ERROR: '手机号不正确',
     PWD_INPUT_EMPTY: '请输入密码',
     CODE_INPUT_EMPTY: '请输入图片验证码',
     CODE_INPUT_ERROR: '验证码不正确',
     LOGIN_RES_ERROR: '用户名或密码不正确',
     SMSCODE_INPUT_EMPTY: '请输入短信验证码',
     SMSCODE_INPUT_ERROR: '短信验证码不正确',
     /*old*/
     VERIFY_PASSWORD: '需要验证您的密码',
     VERIFY_PASSWORD_SUBMIT: '验证',
     USER_RES_ERROR: '用户名不正确',
     MOBILE_INPUT_EMPTY: '请输入手机号码',
     MOBILE_INPUT_ERROR: '手机号码不正确',
     MOBILE_RES_ERROR: '手机号码不正确',
     PWD_RES_ERROR: '密码不正确',
     CODE_RES_ERROR: '验证码不正确',
     REFRESH_IMG_TIP: '点击图片刷新验证码',
     LOGIN_FORZEN: '此帐号已被冻结，暂时无法登录',
     POPULAR: '常用',
     UNKNOWN_ERROR: '未知错误',
     /*new*/
     USERNAME_OR_PASS_NOT_RIGHT: '用户名或密码不正确',
   },
   url = {

     login: '/antacom/passport/dologin.html',
     code: '/api.php?s=antacom/passport/code.html&w=120&h=36',
     smsLogin: '/antacom/passport/msgCheckSms',
     smsSend: '/antacom/passport/smgSmsSend',
   },

   dm = {
     timer: '',
     count: 60,
   },
   c = {
     username: $('#username'),
     phone: $('#phone'),
     smscode: $('#smscode'),
     usernameBox: $('#region-code'),
     phoneBox: $('#phone_box'),
     smscodeBox: $('.labelbox-smscode'),
     password: $('#pwd'),
     passwordBox: $('#passwordBox'),
     errorBox: $('#error-outcon'),
     errMs: $('#error-outcon .error-con'),
     labelbox: $('.labelbox'),
     eye: $('.pwd-eye'),
     captcha: $('#captcha'),
     codeBox: $('.code_label'),
     code: $('#captcha-code'),
     codeImg: $('#captcha-img'),
     from: $('#login-main-form'),
     smscodeBtn: $('.verify-sendbtn'),
     showCode: false,
     loginType: 0,
   };
 //验证密码
 function verify_pwd() {
   var text = c.password.val();
   var errMs = false;
   if (text == '') {
     errMs = J.PWD_INPUT_EMPTY;
   }
   if (errMs) {
     c.errorBox.show();
     c.errMs.html(errMs);
     c.passwordBox.addClass('error_bg');
     return false;
   }
 }

 // 短信验证码
 function verify_smscode() {
   var smscode = c.smscode.val();
   var errMs = false;
   if (!smscode) {
     errMs = J.SMSCODE_INPUT_EMPTY;
   }
   if (errMs) {
     c.errorBox.show();
     c.errMs.html(errMs);
     c.smscodeBox.addClass('error_bg');
     return false;
   }
 }
 //验证手机号
 function verify_phone() {
   var phone = c.phone.val();
   var errMs = false;
   if (!phone) {
     errMs = J.USER_INPUT_PHONE_EMPTY;
   } else if (phone.length > 11) {
     errMs = J.USER_INPUT_PHONE_ERROR;
   }
   if (errMs) {
     c.errorBox.show();
     c.errMs.html(errMs);
     c.phoneBox.addClass('error_bg');
     return false;
   }
 }
 //验证用户名
 function verify_username() {
   var uname = c.username.val();
   var errMs = false;
   if (uname == '') {
     errMs = J.USER_INPUT_EMPTY;
   } else if (uname.length > 36) {
     errMs = J.USER_INPUT_ERROR;
   }
   if (errMs) {
     c.errorBox.show();
     c.errMs.html(errMs);
     c.usernameBox.addClass('error_bg');
     return false;
   }
 }
 function verify_code() {
   var text = c.code.val();
   var errMs = false;
   if (text == '') {
     errMs = J.CODE_INPUT_EMPTY;
   }
   if (errMs) {
     c.errorBox.show();
     c.errMs.html(errMs);
     c.codeBox.addClass('error_bg');
     return false;
   }
 }
 //清楚错误标注
 function verify_clear() {
   c.labelbox.removeClass('error_bg');
   c.errorBox.hide();
 }

 function changeImg(_this) {
   var time = new Date();
   src = url.code + '&' + time.getTime();
   _this.attr('src', src);
 }

 // changeLoginType(1);
 function changeLoginType(idx) {
   c.loginType = idx;
   $('.login_tabs h6').eq(idx).addClass('active').siblings().removeClass('active');

   if (idx === 1) {
     c.captcha.show();
     c.passwordBox.hide();
     c.smscodeBox.show();
     c.phoneBox.show();
     c.usernameBox.hide();
   } else  {
     c.passwordBox.show();
     c.smscodeBox.hide();
     c.phoneBox.hide();
     c.usernameBox.show();
     if (c.showCode === false) {
       c.captcha.hide();
     }
   }
 }

 function sendSms(e) {
   if (e) e.stopPropagation();
   if (c.smscodeBtn.hasClass('disable')) {
     return false;
   }
   verify_clear();
   if (verify_phone() === false) {
     return false;
   } else if (c.captcha.is(':visible') && verify_code() === false) {
     return false;
   }
   MALL.Common.showLoading();
   $.ajax({
     type: 'POST',
     url: url.smsSend,
     data: {
       phone: c.phone.val(),
       vcode: c.code.val(),
     },
     success: function (data) {
       MALL.Common.hideLoading();
       if (data.status == 0) {
         MALL.Common.message({ text: data.msg, time: 1500 });
         clearInterval(dm.timer);
         dm.count = 60;
         c.smscodeBtn.addClass('disable');
         dm.timer = setInterval(function () {
           if (dm.count <= 1) {
             c.smscodeBtn.html('重新获取');
             c.smscodeBtn.removeClass('disable');
             clearInterval(dm.timer);
           } else {
             dm.count--;
             c.smscodeBtn.html('重新获取（' + dm.count + 's）');
           }
         }, 1000);
       } else {
         MALL.Common.message({ text: J[data.msg] ? J[data.msg] : data.msg, time: 2000 });
       }
       changeImg(c.codeImg);
     },
     error: function () {
       MALL.Common.hideLoading();
       changeImg(c.codeImg);
     },
   });
 }
 function smsLogin() {
   try {
     if (verify_phone() === false) {
       return false;
     } else if (verify_smscode() === false) {
       return false;
     }
     // else if (c.captcha.is(':visible') && verify_code() === false) {
     //   return false;
     // }
     MALL.Common.showLoading();
     $.ajax({
       type: 'POST',
       url: url.smsLogin,
       data: {
         phone: c.phone.val(),
         smscode: c.smscode.val(),
       },
       success: function (data) {
        console.log("anta_pc_login:",data);
         MALL.Common.hideLoading();
         if (data.status == 0) {
           MALL.Common.message({ text: J[data.msg] ? J[data.msg] : data.msg, time: 3000 });
           if (data.msg) {
             c.errorBox.show();
             c.errMs.html(J.LOGIN_RES_ERROR);
             c.usernameBox.addClass('error_bg');
           }
         } else if (data.status == -2) {
           MALL.Common.message({ text: J[data.msg] ? J[data.msg] : data.msg, time: 2000 });
           if (data.msg) {
             c.errorBox.show();
             c.errMs.html(J.SMSCODE_INPUT_ERROR);
             c.smscodeBox.addClass('error_bg');
           }
         } else if (data.status == 3) {
           try {
             var prevUrl = sessionStorage.getItem('anta_pc_before_login');
             location.href = prevUrl || data.url;
             console.log("anta_pc_before_login:",prevUrl);
           } catch (error) {
             console.error(error);
             location.href = data.url;
           }
         }
         changeImg(c.codeImg);
       },
       error: function () {
         MALL.Common.hideLoading();
         changeImg(c.codeImg);
       },
     });
   } catch (error) {
     console.error(error);

   }
 }

 //任何输入框点击
 c.labelbox.click(function () {
   verify_clear();
 });
 c.eye.click(function () {
   if ($(this).hasClass('chk')) {
     c.password.attr('type', 'password');
     $(this).removeClass('chk');
   } else {
     c.password.attr('type', 'text');
     $(this).addClass('chk');
   }
 });
 c.codeImg.click(function () {
   changeImg(c.codeImg);
 });
 //表单提交
 c.from.submit(function () {
   if(c.loginType === 1) {
     // 手机号 验证码 登录
     smsLogin();
     return false;
   }
   if (verify_username() === false) {
     return false;
   } else if (verify_pwd() === false) {
     return false;
   } else if (c.captcha.is(':visible') && verify_code() === false) {
     return false;
   }
   var password = hex_md5(c.password.val());
   MALL.Common.showLoading();
   $.ajax({
     type: 'POST',
     url: url.login,
     data: {
       username: c.username.val(),
       password: password,
       vcode: c.code.val(),
     },
     success: function (data) {
      console.log("anta_pc_login:",data);
       MALL.Common.hideLoading();
       if (data.status == 0) {
         MALL.Common.message({ text: J[data.msg] ? J[data.msg] : data.msg, time: 3000 });
         if (data.msg) {
           c.errorBox.show();
           c.errMs.html(J.LOGIN_RES_ERROR);
           c.usernameBox.addClass('error_bg');
         }
         if (data.showCode) {
           c.showCode = true;
           c.captcha.show();
         }
       } else if (data.status == -1) {
         if (data.msg) {
           c.errorBox.show();
           c.errMs.html(J.CODE_INPUT_ERROR);
           c.code.parent().addClass('error_bg');
         }
         if (data.showCode) {
           c.showCode = true;
           c.captcha.show();
         }
       } else if (data.status == 3) {
         try {
           var prevUrl = sessionStorage.getItem('anta_pc_before_login');
           location.href = prevUrl || data.url;
           console.log("anta_pc_before_login:",prevUrl);
         } catch (error) {
           console.error(error);
           location.href = data.url;
         }
       }
       changeImg(c.codeImg);
     },
     error: function () {
       MALL.Common.hideLoading();
       changeImg(c.codeImg);
     },
   });

   return false;
 });
